import  express  from 'express'
import cors from 'cors'
import userRouter from './router/user_router.js'
import userInfoRouter from './router/userinfo_router.js'
import expressJwt from 'express-jwt'


import config from './config.js'

const app = express()

app.use(cors())
app.use(express.json());



app.use(expressJwt({
secret: config.secretKey}).unless({path: [/^\/api\//]}))

app.use('/api',userRouter)//挂载用户登录路由
app.use('/my', userInfoRouter)//挂载用户信息路由

// 错误中间件
app.use(function (err, req, res, next) {
  // 捕获身份认证失败的错误
  if (err.name === 'UnauthorizedError') {
    return res.status(401).json({
      status: 401,
      message: '身份认证失败！',
    });
  }

  // 其他错误处理逻辑

  res.status(500).json({
    status: 500,
    message: '服务异常',
  });
});

app.listen(80, () => {
  console.log('serve run 127.0.0.1')
})

